package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.IApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author lyd
 * @since 2022-06-20
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements IApAuthorService {

    @Override
    public ResponseResult<ApAuthor> saveApAuthor(ApAuthor apAuthor) {
        //关键数据校验
        if (apAuthor == null || apAuthor.getUserId() ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断该作者是否已经存在 存在直接返回作者信息
        LambdaQueryWrapper<ApAuthor> query = new LambdaQueryWrapper<>();
        query.eq(ApAuthor::getUserId,apAuthor.getUserId());
        ApAuthor one = this.getOne(query);
        if (one != null){
            return ResponseResult.okResult(one);
        }
        //不存在新增作者信息
        apAuthor.setCreatedTime(new Date());
        this.save(apAuthor);

        return ResponseResult.okResult();
    }
}
